//
//  NetWorkTool.m
//  QuickCure
//
//  Created by 马文星 on 16/4/25.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "NetWorkTool.h"

@implementation NetWorkTool


+ (instancetype)shareManager{

    static NetWorkTool *manager = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        
        //1.configuration
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        //2.AFNSessionManager
        manager = [[NetWorkTool alloc] initWithSessionConfiguration:configuration];
        
        //3.返回类型
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //4.请求格式
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //5.超时
        manager.requestSerializer.timeoutInterval = 10.0;
    });

    return manager;
}


- (BOOL)isReachableByAFN{

    
   Reachability *reachability = [Reachability reachabilityWithHostName:CZ_BaseUrl];
    
    return reachability.isReachable;
}


- (void)GET_RequestWithUrlString:(NSString *)urlString
                          parmas:(NSDictionary *)params
                   successBloock:(SuccessCompletedBlock)success
                    failureBlock:(FailureCompletedBlock)failure{

    [[NetWorkTool shareManager] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
}


- (void)POST_RequestWithUrlString:(NSString *)urlString
                          parmas:(NSDictionary *)params
                   successBloock:(SuccessCompletedBlock)success
                     failureBlock:(FailureCompletedBlock)failure{
    
    [[NetWorkTool shareManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
    }];
}




@end
